<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //手写call
        function fn(name) {
            console.log(name);
        }
        function fn2(FirstName,LastName){
            console.log(FirstName,LastName);
        }
        Function.prototype.myCall = function (o) {
            console.log(o);
            let obj = o || window;
            obj.fn = this;
            let args = [...arguments].slice(1);
            let res = obj.fn(...args);
            delete obj.fn;
            return res;
        }

        fn.myCall(undefined, "ctx")
        //手写apply
        Function.prototype.myApply = function(obj){
            let ctx = obj || window;
            ctx.fn = this;
            let args = [];
            // console.log(arguments[1]);
            if(arguments.length > 1){
                args = arguments[1];
            }
            let res = ctx.fn(...args);
            delete ctx.fn;
            return res;
        }
        fn2.myApply(undefined,["ctx","dwa"]);

        //手写bind
        function add(y){
            return this.x + y; 
        }
        Function.prototype.myBind = function(obj){
            let ctx = obj || window;
            let self = this;
            let args = [...arguments].slice(1);
            return function(){
                let newArgs = [...arguments];
                return self.apply(ctx,args.concat(newArgs));
            }
        }
        console.log(add.myBind({x:1},2)());

    </script>
</body>

</html>